﻿using COSXML.Network;
using Kalman.Extensions;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System.Xml.Linq;

namespace AdminUI
{
    /// <summary>
    /// Grid控件查询数据对象
    /// </summary>
    /// <typeparam name="TKey">主键值类型</typeparam>
    public class QueryData<TKey>
    {
        /// <summary>
        /// 操作类型（delete,audit等）
        /// </summary>
        public string ActionType { get; set; }

        /// <summary>
        /// 选中的记录主键值数组
        /// </summary>
        public TKey[] SelectedIDs { get; set; }

        /// <summary>
        /// 选中的记录主键值（多选时为第一个）
        /// </summary>
        public TKey SelectedID { get { return SelectedIDs.Length > 0 ? SelectedIDs[0] : default; } }

        /// <summary>
        /// 当前页索引，从0开始
        /// </summary>
        [BindProperty(Name = "Grid1_pageIndex")]
        public int PageIndex { get; set; }

        /// <summary>
        /// 每页记录数（当前数据值）
        /// </summary>
        [BindProperty(Name = "Grid1_pageSize")]
        public int PageSize { get; set; }

        /// <summary>
        /// 每页记录数（改变后的数据值）
        /// </summary>
        [BindProperty(Name = "ddlGridPageSize")]
        public int ChangedPageSize { get; set; }

        /// <summary>
        /// 排序字段
        /// </summary>
        [BindProperty(Name = "Grid1_sortField")]
        public string SortField { get; set; }

        /// <summary>
        /// 排序方向（ASC-升序，DESC-降序）
        /// </summary>
        [BindProperty(Name = "Grid1_sortDirection")]
        public string SortDirection { get; set; }

        /// <summary>
        /// 当前表格控件所有列名称数组（注意：事件触发控件需要指定表格所在容器ID）
        /// </summary>
        [BindProperty(Name = "Grid1_fields")]
        public string[] Fields { get; set; }

        /// <summary>
        /// 排序表达式
        /// </summary>
        public string SortExp
        {
            get
            {
                if (SortField.IsNullOrEmpty())
                {
                    return string.Empty;
                }
                else
                {
                    return $"{SortField} {SortDirection}";
                }
            }
        }

        /// <summary>
        /// 每页记录数是否改变
        /// </summary>
        public bool IsChangedPageSize
        {
            get { return PageSize != ChangedPageSize; }
        }

        /// <summary>
        /// 已修改数据集合（修改状态status：修改-modified，新增-newadded，删除-deleted）
        /// <para>示例数据：
        /// <![CDATA[[
        ///     [
        ///         {"index":0,"values":{"Memo":"1111"},"status":"modified","originalIndex":0,"id":"146","text":"aaa"},
        ///         {"index":1,"values":{"Memo":"2222"},"status":"newadded","originalIndex":1,"id":"79","text":"bbb"}
        ///     ]
        /// ]]>
        /// </para>
        /// </summary>
        [BindProperty(Name = "Grid1_modifiedData")]
        public JArray ModifiedData { get; set; }
    }

}
